package com.example.springredis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Array;
import java.util.*;

@Controller
public class MyController {
    @Autowired
    StringRedisTemplate redisTemplate;

    @ResponseBody
    @GetMapping("/testString")
    public String testString() {
        redisTemplate.opsForValue().set("k1", "111");
        redisTemplate.opsForValue().set("k2", "222");
        redisTemplate.opsForValue().set("k3", "333");

        String value = redisTemplate.opsForValue().get("k1");
        System.out.println("value: " + value);

        Boolean del = redisTemplate.delete("k1");
        System.out.println("del: " + del);

        List<String> keys = Arrays.asList("k1", "k2", "k3");
        List<String> values = redisTemplate.opsForValue().multiGet(keys);
        System.out.println(values);
        return "ok";
    }

    @ResponseBody
    @GetMapping("/testList")
    public String testList() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushDb();
            return null;
        });

        redisTemplate.opsForList().leftPush("key", "111");
        redisTemplate.opsForList().leftPush("key", "222");
        redisTemplate.opsForList().leftPush("key", "333");

        List<String> result = redisTemplate.opsForList().range("key", 0, -1);
        System.out.println("result: " + result);

        Long size = redisTemplate.opsForList().size("key");
        System.out.println("size: " + size);

        Long remove = redisTemplate.opsForList().remove("key", 1, "111");
        System.out.println("remove: " + remove);

        size = redisTemplate.opsForList().size("key");
        System.out.println("size: " + size);

        String value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value: " + value);

        value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value: " + value);

        value = redisTemplate.opsForList().rightPop("key");
        System.out.println("value: " + value);
        return "ok";
    }

    @ResponseBody
    @GetMapping("/testSet")
    public String testSet() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushDb();
            return null;
        });

        redisTemplate.opsForSet().add("key", "111", "222", "333", "444", "555");
        Long size = redisTemplate.opsForSet().size("key");
        System.out.println("size: " + size);


        Boolean exists = redisTemplate.opsForSet().isMember("key", "111");
        System.out.println("exists: " + exists);
        exists = redisTemplate.opsForSet().isMember("key", "222");
        System.out.println("exists: " + exists);

        Long remove = redisTemplate.opsForSet().remove("key","111","333","777");
        System.out.println("remove: " + remove);

        exists = redisTemplate.opsForSet().isMember("key", "111");
        System.out.println("exists: " + exists);
        exists = redisTemplate.opsForSet().isMember("key", "222");
        System.out.println("exists: " + exists);

        size = redisTemplate.opsForSet().size("key");
        System.out.println("size: " + size);
        return "ok";
    }

    @ResponseBody
    @GetMapping("/testHash")
    public String testHash() {
        redisTemplate.execute((RedisConnection connection) -> {
           connection.flushDb();
           return null;
        });

        redisTemplate.opsForHash().put("key", "f1","111");
        redisTemplate.opsForHash().put("key", "f2","222");
        redisTemplate.opsForHash().put("key", "f3","333");
        redisTemplate.opsForHash().put("key", "f4","444");

        String value = (String) redisTemplate.opsForHash().get("key", "f1");
        System.out.println("value: " + value);


        Boolean hasKey = redisTemplate.opsForHash().hasKey("key", "f1");
        System.out.println("hasKey: " + hasKey);

        Long size = redisTemplate.opsForHash().size("key");
        System.out.println("size: " + size);

//        Boolean del = redisTemplate.delete("key");
        Long del = redisTemplate.opsForHash().delete("key", "f1", "f3");
        System.out.println("del: " + del);


        size = redisTemplate.opsForHash().size("key");
        System.out.println("size: " + size);

        List<Object> keys = Arrays.asList("f1","f2","f3","f4");
        List<Object> result = redisTemplate.opsForHash().multiGet("key", keys);
        System.out.println("result: " + result);

        return "ok";
    }

    @ResponseBody
    @GetMapping("/testZSet")
    public String testZSe() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushDb();
            return null;
        });

        redisTemplate.opsForZSet().add("key", "zhangsan", 90);
        redisTemplate.opsForZSet().add("key", "lisi", 80);
        redisTemplate.opsForZSet().add("key", "wangwu", 99);

        Set<ZSetOperations.TypedTuple<String>> rangeByScoreWithScores = redisTemplate.opsForZSet().rangeByScoreWithScores("key", 0, 100);
        System.out.println("rangeByScoreWithScores: " +rangeByScoreWithScores);

        Long len = redisTemplate.opsForZSet().count("key",0,100);
        System.out.println("len: " + len);

        Set<String> range = redisTemplate.opsForZSet().range("key", 0, -1);
        System.out.println("range: " + range);

        ZSetOperations.TypedTuple max = redisTemplate.opsForZSet().popMax("key");
        System.out.println("max: " + max);


        max = redisTemplate.opsForZSet().popMax("key");
        System.out.println("max: " + max);

        Long rank = redisTemplate.opsForZSet().rank("key", "lisi");
        System.out.println("rank: " + rank);



        return "ok";
    }
}
